package jxc.servlet.user;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.log.LogFactory;
import eu.log.Logger;

import jxc.base.helper.UserAuthenticator;
import jxc.data.hibernate.dao.OperatorDAO;
import jxc.data.hibernate.pojo.Operator;

/**
 * 删除用户
 */
@WebServlet("/user/delete")
public class Delete extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger log=LogFactory.getLogger("操作员管理.删除");
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Delete() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * 删除用户
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username=request.getParameter("uid");
		OperatorDAO opDAO=new OperatorDAO();
		Operator op=opDAO.getByUsername(username);
		if(op==null){
			response.sendError(500,"User Not Found");
			return;
		}
		if(UserAuthenticator.getUser(request).getUsername().equals(op.getUsername())){
			request.setAttribute("message", "不要把自己删除了，这种玩笑开不起");
			request.getRequestDispatcher("/user/listUser").forward(request, response);
			return;
		}
		opDAO.delete(op);
		opDAO.commit();
		log.notice("用户["+username+"] 被删除");
		request.getRequestDispatcher("/user/listUser").forward(request, response);
	}

}
